# -*- coding: utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2JSON import Table2Json
import time

PAGE_SIZE = 30
local_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

'''
姚明 281720252@qq.com
'''


class IndentAPI(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections()  # 初始化数据库链接

    def queryIndent(self, page_index, userId):  # zzh_declar表查询记录

        cur = self.conn.cursor()
        sql = "select * from zzh_indent,zzh_item WHERE user_id= %s AND zzh_indent.item_id=zzh_item.item_id"
        total_count = cur.execute(sql, [userId])
        indentList = cur.fetchall()

        cur.close()
        self.conn.close()

        if (page_index * PAGE_SIZE) < total_count:
            start = (page_index - 1) * PAGE_SIZE
            end = page_index * PAGE_SIZE
            indentListPart = indentList[start: end]

            indentListJson = Table2Json.indent_and_item_to_json(indentListPart)
            return total_count, indentListJson
        else:
            start = (page_index - 1) * PAGE_SIZE
            indentListPart = indentList[start:]
            indentListJson = Table2Json.indent_and_item_to_json(indentListPart)
            return total_count, indentListJson

    def queryIndentByStatus(self, page_index, userId, indentStatus):  # zzh_declar表查询记录

        cur = self.conn.cursor()
        sql = "select * from zzh_indent,zzh_item WHERE (user_id= %s AND indent_status=%s AND zzh_indent.item_id=zzh_item.item_id)"
        total_count = cur.execute(sql, [userId, indentStatus])
        indentList = cur.fetchall()
        cur.close()
        self.conn.close()

        if (page_index * PAGE_SIZE) < total_count:
            start = (page_index - 1) * PAGE_SIZE
            end = page_index * PAGE_SIZE
            indentListPart = indentList[start: end]

            indentListJson = Table2Json.indent_and_item_to_json(indentListPart)
            return total_count, indentListJson
        else:
            start = (page_index - 1) * PAGE_SIZE
            indentListPart = indentList[start:]
            indentListJson = Table2Json.indent_and_item_to_json(indentListPart)
            return total_count, indentListJson

    def addIndent(self, addIndent):  # process表添加记录
        userId = addIndent['userId']
        indentSum = addIndent['indentSum']
        indentMessage = addIndent['indentMessage']
        indentCode = addIndent['indentCode']
        indentStatus = addIndent['indentStatus']
        invoiceId = addIndent['invoiceId']
        itemId = addIndent['itemId']
        processCode = addIndent['processCode']
        indentTime = local_time
        cur = self.conn.cursor()
        sql = "INSERT INTO zzh_indent (user_id,indent_sum,indent_message,indent_code,indent_status,invoice_id,item_id,process_code,indent_time) values (%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        addDeclarResultOfDatabase = cur.execute(sql,
                                                [userId, indentSum, indentMessage, indentCode, indentStatus, invoiceId,
                                                 itemId, processCode, indentTime])
        self.conn.commit()
        if addDeclarResultOfDatabase:  # 插入成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 插入失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def updateIndentById(self, newUpdateIndent, indentId):
        userId = newUpdateIndent['userId']
        indentSum = newUpdateIndent['indentSum']
        indentMessage = newUpdateIndent['indentMessage']
        indentCode = newUpdateIndent['indentCode']
        indentStatus = newUpdateIndent['indentStatus']
        invoiceId = newUpdateIndent['invoiceId']
        itemId = newUpdateIndent['itemId']
        processCode = newUpdateIndent['processCode']
        indentTime = local_time
        cur = self.conn.cursor()
        sql = "UPDATE zzh_indent SET user_id=%s,indent_sum=%s,indent_message=%s ,indent_code=%s ,indent_status=%s,invoice_id=%s,item_id=%s,process_code=%s,indent_time=%s WHERE indent_id=%s"
        updateDeclarResultOfDatabase = cur.execute(sql, [userId, indentSum, indentMessage, indentCode, indentStatus,
                                                         invoiceId, itemId, processCode, indentTime, indentId])
        self.conn.commit()
        if updateDeclarResultOfDatabase:  # 更新成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 更新失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def deleteIndentById(self, indentId):
        cur = self.conn.cursor()
        sql = "DELETE from zzh_indent WHERE indent_id=%s"
        deleteIndentResultOfDatabase = cur.execute(sql, [indentId])
        self.conn.commit()
        if deleteIndentResultOfDatabase:  # 删除成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 删除失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def queryIndentByIndentId(self, indetId):
        cur = self.conn.cursor()
        sql = "select * from zzh_indent,zzh_item WHERE indent_id=%s AND zzh_indent.item_id=zzh_item.item_id"
        total_count = cur.execute(sql, [indetId])
        indentList = cur.fetchall()
        cur.close()
        self.conn.close()
        indentListJSON = Table2Json.indent_and_item_to_json(indentList)
        return total_count, indentListJSON

    def query_indent(self, indent_id):
        cur = self.conn.cursor()
        sql = "select * from zzh_indent WHERE indent_id=%s"

        rs = cur.execute(sql, [indent_id])
        indentList = cur.fetchone()
        cur.close()
        self.conn.close()

        return indentList

    def queryIndetnInfo(self, userId, itemId):
        cur = self.conn.cursor()
        sql = "select indent_id,indent_status from zzh_indent WHERE user_id=%s AND item_id=%s"
        rs = cur.execute(sql, [userId, itemId])
        indentIdAndIndentStatus = cur.fetchall()
        indentIdAndIndentStatusJSON = Table2Json.indentIdAndStatus_list_to_json(indentIdAndIndentStatus)
        if rs:
            return indentIdAndIndentStatusJSON
        else:
            return None
